package com.example.prison.service.impl;

import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.prison.config.LoginInterceptor;
import com.example.prison.entity.CarManagement;
import com.example.prison.service.CarManagementService;
import com.example.prison.mapper.CarManagementMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;

/**
* @author jiang
* @description 针对表【car_management】的数据库操作Service实现
* @createDate 2024-06-03 11:07:19
*/
@Service
public class CarManagementServiceImpl extends ServiceImpl<CarManagementMapper, CarManagement>
    implements CarManagementService{

    @Autowired
    private CarManagementMapper carManagementMapper;

    @Override
    public PageInfo<CarManagement> get(int pageNumber, int pageSize, String carNumber, String carDepart,String carStatus) {
//        //非超级管理员权限
//        if(!Objects.equals(LoginInterceptor.loginUser.getRoleName(), "超级管理员")) {
//            PageHelper.startPage(pageNumber, pageSize);
//            return new PageInfo<>(carManagementMapper.get(carNumber, LoginInterceptor.loginUser.getDepartment(), carStatus));
//        }
        PageHelper.startPage(pageNumber, pageSize);
        return new PageInfo<>(carManagementMapper.get(carNumber, carDepart, carStatus));
    }

    @Override
    public List<CarManagement> getInsideCar(){
        return carManagementMapper.getInsideCar();
    }

    @Override
    public List<CarManagement> getOutsideCar(){
        return carManagementMapper.getOutsideCar();
    }

    @Override
    public int add(CarManagement car) {
        String carNumber = car.getCarNumber();
        if (ObjectUtils.isNotEmpty(carManagementMapper.getOneByNumber(carNumber))) {
            return -1;
        }
//        else if(!Objects.equals(LoginInterceptor.loginUser.getRoleName(), "超级管理员") && !Objects.equals(LoginInterceptor.loginUser.getDepartment(), car.getCarDepart())) {
//            //非超级管理员权限
//            return 0;
//        }
        else {
            carManagementMapper.add(car);
            return 1;
        }
    }

    @Override
    public void delete(Integer[] ids) {
        System.out.println(Arrays.toString(ids));
        carManagementMapper.deleteCar(ids);
    }

    @Override
    public int update(CarManagement car, Integer carId) {
        CarManagement carDTO = carManagementMapper.getById(carId);
        carManagementMapper.updateCar(car, carId);
        if(Objects.equals(carDTO.getCarStatus(), "营区外")) {
            return 0;
        } else if (carManagementMapper.getListOneByNumber(car.getCarNumber()).size() > 1) {
            carManagementMapper.updateCar(carDTO, carId);
            return -1;
        } else {
            return 1;
        }
    }

    @Override
    public CarManagement getById(Integer departId) {
        return carManagementMapper.getById(departId);
    }

    @Override
    public CarManagement getOneByNumber(String carNumber) {
        return carManagementMapper.getOneByNumber(carNumber);
    }

    @Override
    public CarManagement getByNumberAndDepart(String carNumber, String depart) {
        return carManagementMapper.getByNumberAndDepart(carNumber,depart);
    }

    @Override
    public PageInfo<CarManagement> getRetentionCar(int pageNumber, int pageSize, String carNumber, String depart) {
        return new PageInfo<>(carManagementMapper.getRetentionCar(carNumber, depart));
    }

}




